package com.sphd.emanu.activity.vipdetails;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.pointscenter.PointscenterActivity;
import com.sphd.emanu.activity.toptpcenter.ToptpcenterActivity;
import com.sphd.emanu.adapter.VipOrderAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.order.Order;
import com.sphd.emanu.pojo.vip.MemBalance;
import com.sphd.emanu.pojo.vip.Member;
import com.sphd.emanu.pojo.vip.MemberForOrg;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.BigDisMailUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 会员模块------会员详情
 *
 * @Author Hu
 * @Time 2017/5/16 -- 15:48.
 * @Email 87683202@qq.com
 */
public class VipdetailsActivity extends MVPBaseActivity<VipdetailsContract.View, VipdetailsPresenter> implements VipdetailsContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    Member member;
    @BindView(R.id.vip_img)
    ImageView vipImg;
    @BindView(R.id.vip_name)
    TextView vipName;
    @BindView(R.id.vip_no)
    TextView vipNo;
    @BindView(R.id.top_up_money)
    TextView topUpMoney;
    @BindView(R.id.coupon)
    TextView coupon;
    @BindView(R.id.points)
    TextView points;
    User user;
    VipOrderAdapter vipOrderAdapter;
    @BindView(R.id.all_order_count_money)
    TextView allOrderCountMoney;
    View coupView;
    AlertDialog coupDialog;
    TextView title;
    MaterialEditText myEdit;
    Button btnConfirm, btnCancle;
    List<Moudle> moudles;
    boolean isCanDo = false;
    MemberForOrg memberForOrg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vipdetails);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());
        toolbar.setNavigationOnClickListener(v -> finish());
        btnCancle.setOnClickListener(v -> coupDialog.dismiss());
        btnConfirm.setOnClickListener(v -> {
            if (TextUtils.isEmpty(myEdit.getText().toString().trim())) {
                toast("请输入优惠券的数量后提交。");
            } else {
                showProgressDialog();
                mPresenter.addCouponForMember(member.getId(), Integer.valueOf(myEdit.getText().toString().trim()), user.getOid());
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_VIP_6);
        member = (Member) bundle.getSerializable(Constant.MEMBER_ENTITY);
        setTitleBar(toolbar, member.getName());
        initDialog();
        setRefresh(mySwf);
        initAdapter();
        initTv();
        addData();
    }

    private void initDialog() {
        coupView = View.inflate(context, R.layout.dialog_edit_myself, null);
        title = (TextView) coupView.findViewById(R.id.title);
        myEdit = (MaterialEditText) coupView.findViewById(R.id.my_edit);
        btnConfirm = (Button) coupView.findViewById(R.id.btn_confirm);
        btnCancle = (Button) coupView.findViewById(R.id.btn_cancle);
        coupDialog = getDialogBuilder(coupView, R.style.AlertDialog);
        title.setText("赠送优惠券");
        myEdit.setHint("请输入赠送优惠券的数量");

    }

    private void initAdapter() {
        setRecy(myRecycler);
        vipOrderAdapter = new VipOrderAdapter(R.layout.item_vip_order, new ArrayList<Order>(),context);
       vipOrderAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(vipOrderAdapter);
    }

    private void addData() {
        showProgressDialog();
        mPresenter.findMember(user.getOid(), member.getId());
    }

    private void initTv() {
        vipName.setText(member.getName());
        vipNo.setText(TextUtils.isEmpty(member.getMobile()) ? "" : member.getMobile());
    }

    @Override
    public void recBean(MemberForOrg memberForOrg) {
        dissProgressDialog();
        this.memberForOrg=memberForOrg;
        MemBalance memBalance = memberForOrg.getMemBalance();
        topUpMoney.setText("充值卡余额\n￥" + (memBalance.getDeposit() == null ? "0" :
                memBalance.getDeposit().toString()));
        coupon.setText("优惠券\n" + (memBalance.getCouponNumber() == null ? "0" : BigDisMailUtils.subZeroAndDot(String.valueOf(memBalance.getCouponNumber()))));
        points.setText("积分\n" + (memBalance.getPoints() != null ? memBalance.getPoints() : 0));
        vipOrderAdapter.setNewData(memberForOrg.getOrders());
        if (vipOrderAdapter.getData().size() != 0) {
            allOrderCountMoney.setText("共消费" + memberForOrg.getOrderCount() + "次共消费" + memberForOrg.getOrderSum() + "元");
        } else {
            allOrderCountMoney.setText("暂无消费记录");
        }
    }

    @OnClick({R.id.top_up_money, R.id.coupon, R.id.points})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_up_money:

                bundle.putSerializable(Constant.MEMBER_FOR_ORG,memberForOrg);
                intent.putExtras(bundle);
                intent.setClass(context, ToptpcenterActivity.class);
                startActivity(intent);
                break;
            case R.id.coupon:
                if (isCanDo)
                    coupDialog.show();
                break;
            case R.id.points:
                intent.setClass(context, PointscenterActivity.class);
                startActivity(intent);

                break;
        }
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast(R.string.up_success);
            coupDialog.dismiss();
            addData();
        } else {
            toast(R.string.up_failure);
        }
    }
}
